package com.tjs.lucky.domain.activity.service.deploy;

import com.tjs.lucky.domain.activity.model.req.ActivityConfigReq;
import com.tjs.lucky.domain.activity.model.aggregates.ActivityConfigRich;
import com.tjs.lucky.domain.activity.model.vo.ActivityVO;
import com.tjs.lucky.domain.activity.model.vo.AwardVO;
import com.tjs.lucky.domain.activity.model.vo.StrategyDetailVO;
import com.tjs.lucky.domain.activity.model.vo.StrategyVO;
import com.tjs.lucky.domain.activity.repository.IActivityRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 田佳澍
 * @Description: 创建活动配置服务
 * @DateTime: 2023/5/22 16:04
 **/
@Service
public class ActivityDeploy {

    private final Logger logger = LoggerFactory.getLogger(ActivityDeploy.class);

    @Resource
    private IActivityRepository activityRepository;

    @Transactional(rollbackFor = Exception.class)
    public void createActivityFullConfig(ActivityConfigReq req) {
        ActivityConfigRich activityConfigRich = req.getActivityConfigRich();
        // 添加活动配置
        createActivity(activityConfigRich.getActivity());

        // 添加奖品配置
        createAwardList(activityConfigRich.getAwardList());

        // 添加策略配置
        createStrategy(activityConfigRich.getStrategy());
        logger.info("创建活动配置完成，activityId：{}", req.getActivityId());

    }


    public void createActivity(ActivityVO activity) {
        activityRepository.addActivity(activity);
        logger.info("创建活动配置完成，activityId：{}", activity.getActivityId());
    }

    @Transactional(rollbackFor = Exception.class)
    public void createStrategy(StrategyVO strategy) {
        activityRepository.addStrategy(strategy);
        List<StrategyDetailVO> strategyDetailList = strategy.getStrategyDetailList();
        // 添加策略详细配置
        activityRepository.addStrategyDetailList(strategyDetailList);
        logger.info("创建策略配置完成，strategyId：{}", strategy.getStrategyId());
    }

    public void createAwardList(List<AwardVO> awardList) {
        activityRepository.addAward(awardList);
        logger.info("创建奖品配置完成！");
    }

//    public void updateActivity(ActivityConfigReq req) {
//        // TODO: 非核心功能后续补充
//    }

    public List<ActivityVO> scanToDoActivityList(Long id) {
        return activityRepository.scanToDoActivityList(id);
    }
}
